package com.soxing.braindata.service.stat;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.soxing.braindata.dao.domain.request.ResearchSearchRequest;
import com.soxing.braindata.dao.domain.vo.TinyResearch;
import com.soxing.braindata.dao.entity.StatItemResult;
import com.soxing.braindata.dao.entity.Testee;
import com.soxing.braindata.dao.entity.TesteeExperiment;
import com.soxing.braindata.dao.service.IStatItemResultService;
import com.soxing.braindata.dao.service.ITesteeExperimentService;
import com.soxing.braindata.dao.service.ITesteeService;
import com.soxing.braindata.domain.enumeration.ComplianceRateEnum;
import com.soxing.braindata.domain.enumeration.QualityRateEnum;
import com.soxing.braindata.service.manager.IResearchManager;
import com.soxing.common.utils.response.MyPage;
import com.soxing.common.utils.response.WebResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service("qualityRateServiceImpl")
public class QualityRateServiceImpl implements IStatService{
    @Resource
    private IResearchManager researchManager;

    @Resource
    private IStatItemResultService statItemResultService;

    @Resource
    private ITesteeExperimentService testeeExperimentService;

    @Resource
    private ITesteeService testeeService;


    @Override
    public void stat(Date beginTime, Date endTime, Integer period) {
        ResearchSearchRequest request = new ResearchSearchRequest();
        List<Integer> stateList = Lists.newArrayList();
        stateList.add(1);
        stateList.add(3);
        request.setStateList(stateList);
        request.setPageSize(10000);
        WebResponse<MyPage<TinyResearch>> response = researchManager.search(request);
        List<TinyResearch> records = response.getData().getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return;
        }
        Set<String> subjectPkIdSet = records.stream().map(TinyResearch::getSubjectPkId).collect(Collectors.toSet());
        //上面这一段代码都是为了获得一个研究码的集合
        //List<Testee> testeeList = testeeService.list(new LambdaQueryWrapper<Testee>().in(Testee::getSubjectPkId, subjectPkIdSet));
        List<TesteeExperiment> testeeExperimentList = testeeExperimentService.list(new LambdaQueryWrapper<TesteeExperiment>().in(TesteeExperiment::getSubjectPkId, subjectPkIdSet));
        if (CollectionUtils.isEmpty(testeeExperimentList)) {
            return;
        }
        Map<String, List<TesteeExperiment>> subjectPkId2TesteeListMap = new HashMap<>();
        for (TesteeExperiment testeeExperiment : testeeExperimentList) {
            String subjectPkId = testeeExperiment.getSubjectPkId();
            List<TesteeExperiment> testeeList1 = subjectPkId2TesteeListMap.get(subjectPkId);
            if (testeeList1 == null) {
                testeeList1 = new ArrayList<>();
                subjectPkId2TesteeListMap.put(subjectPkId, testeeList1);
            }
            testeeList1.add(testeeExperiment);
        }

        //对于每个中心还要再统计一下的。
        subjectPkId2TesteeListMap.forEach((rcode, tList) ->{


            Map<String,List<TesteeExperiment>> sitePkIdTesteeListMap = tList.stream().collect(Collectors.groupingBy(
                    x -> testeeService.getOne(new LambdaQueryWrapper<Testee>().eq(Testee::getTesteeNo,x.getTesteeNo())).getSitePkId()
            ));

            sitePkIdTesteeListMap.forEach((sitePkId,sList)->{

                Long low = sList.stream().filter(a -> a.getDataQuality() == 1).count();
                Long middle = sList.stream().filter(a -> a.getDataQuality() == 2).count();
                Long heigh = sList.stream().filter(a -> a.getDataQuality() == 3).count();


                Date now = new Date();
                //可以这样写的，如果是分中心code为0 就是对整个队列的统计，这样没这么麻烦了。
                StatItemResult statItemResult1 = new StatItemResult();
                statItemResult1.setItemCode(QualityRateEnum.LOW.getKey());
                statItemResult1.setItemName(QualityRateEnum.LOW.getName());
                statItemResult1.setSubjectPkId(rcode);
                statItemResult1.setSitePkId(sitePkId);
                statItemResult1.setStatResult(BigDecimal.valueOf(low));
                statItemResult1.setCreateTime(now);

                StatItemResult statItemResult2 = new StatItemResult();
                statItemResult2.setItemCode(QualityRateEnum.MIDDLE.getKey());
                statItemResult2.setItemName(QualityRateEnum.MIDDLE.getName());
                statItemResult2.setSubjectPkId(rcode);
                statItemResult2.setSitePkId(sitePkId);
                statItemResult2.setStatResult(BigDecimal.valueOf(middle));
                statItemResult2.setCreateTime(now);


                StatItemResult statItemResult3 = new StatItemResult();
                statItemResult3.setItemCode(QualityRateEnum.HIGH.getKey());
                statItemResult3.setItemName(QualityRateEnum.HIGH.getName());
                statItemResult3.setSubjectPkId(rcode);
                statItemResult3.setSitePkId(sitePkId);
                statItemResult3.setStatResult(BigDecimal.valueOf(heigh));
                statItemResult3.setCreateTime(now);


                statItemResultService.remove(new LambdaQueryWrapper<StatItemResult>()
                        .eq(StatItemResult::getSubjectPkId, rcode)
                        .eq(StatItemResult::getSitePkId, sitePkId)
                        .in(StatItemResult::getItemCode, QualityRateEnum.HIGH.getKey(), QualityRateEnum.MIDDLE.getKey(),QualityRateEnum.LOW.getKey()));

                statItemResultService.saveBatch(Lists.newArrayList(statItemResult1, statItemResult2,statItemResult3));


            });




        });

    }
}
